package com.uni.taggroupview;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class TagGroupView extends LinearLayout {
    String[] defultText;
    private int selectTextColor;
    /** 未选中,未打开时的图标 */
    private Drawable mDIocn;
    /** 未选中,打开时的图标 */
    private Drawable mUpIocn;

    /** 已选中后,未打开时的图标 */
    private Drawable mDRedIocn;
    /** 已选中后，打开时的图标 */
    private Drawable mUpRedIcon;


    List<TagGroup> tagGroups;
    private int clickIndex=-1;

    private OnTagClickListener onTagClickListener;


    public TagGroupView(Context context) {
        super(context);
        init();
    }

    public TagGroupView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TagGroupView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init(){
        selectTextColor= Color.RED;
        mDIocn=getResources().getDrawable(R.mipmap.ic_gray_down,null);
        mUpIocn=getResources().getDrawable(R.mipmap.ic_gray_up,null);

        mDRedIocn=getResources().getDrawable(R.mipmap.ic_red_down,null);
        mUpRedIcon=getResources().getDrawable(R.mipmap.ic_red_up,null);
    }

    public void setData(String[] text){
        tagGroups=new ArrayList<>();
        defultText=text;
        for (String s : text) {
            tagGroups.add(new TagGroup(s));
        }
        for (int i = 0; i < tagGroups.size(); i++) {
            TagGroup tagGroup=tagGroups.get(i);
            View view= LayoutInflater.from(getContext()).inflate(R.layout.tag_group_view, null, true);
            TextView textView = view.findViewById(R.id.textView);

            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            params.weight = 1.0f;
            params.gravity = Gravity.CENTER;
            view.setLayoutParams(params);

            textView.setText(tagGroup.text);
            textView.setCompoundDrawablesWithIntrinsicBounds(null, null, mDIocn, null);
            int index = i;
            view.setOnClickListener(v -> {
                clickIndex= index;
                if(!tagGroup.isOpen()){
                    tagGroup.setOpen(true);
                    textView.setCompoundDrawablesWithIntrinsicBounds(null, null, getOpenIcon(), null);
                }else{
                    tagGroup.setOpen(false);
                    textView.setCompoundDrawablesWithIntrinsicBounds(null, null, getCloseIcon(), null);
                }
                if(onTagClickListener!=null) onTagClickListener.onClick(view,index,tagGroup.isOpen());
            });
            addView(view);
        }
    }
    // 传入选中值后，三角形需要恢复-是否选中需要与初始值判断
    public void selectTextHighLight(String string) {
        if(string==null || string.length()==0 || defultText==null || clickIndex==-1) return;
        String defultValue=defultText[clickIndex];
        TextView textView=getChildAt(clickIndex).findViewById(R.id.textView);
        textView.setText(string);
        // 与初始值不同，1.需要红色高亮 2.
        if(!defultValue.equals(string)){
            textView.setTextColor(selectTextColor);
        }else {
            textView.setTextColor(Color.BLACK);
        }
        textView.setCompoundDrawablesWithIntrinsicBounds(null, null, getCloseIcon(), null);
        tagGroups.get(clickIndex).setOpen(false);
    }
    // 恢复默认值、颜色
    public void defaultStatus() {
        String defultValue=defultText[clickIndex];
        TextView textView=getChildAt(clickIndex).findViewById(R.id.textView);
        textView.setText(defultValue);
        textView.setTextColor(Color.BLACK);
        textView.setCompoundDrawablesWithIntrinsicBounds(null, null, mDIocn, null);
        tagGroups.get(clickIndex).setOpen(false);
    }

    // 选中后，再次打开，然后不选中，点击空白处
    public void closeIocn() {
        TextView textView=getChildAt(clickIndex).findViewById(R.id.textView);
        textView.setCompoundDrawablesWithIntrinsicBounds(null, null, getCloseIcon(), null);
        tagGroups.get(clickIndex).setOpen(false);
    }


    private Drawable getCloseIcon(){
        String defultValue=defultText[clickIndex];
        TextView textView=getChildAt(clickIndex).findViewById(R.id.textView);
        return textView.getText().equals(defultValue) ? mDIocn : mDRedIocn;
    }

    private Drawable getOpenIcon(){
        String defultValue=defultText[clickIndex];
        TextView textView=getChildAt(clickIndex).findViewById(R.id.textView);
        return textView.getText().equals(defultValue) ? mUpIocn : mUpRedIcon;
    }

    public int getSelectTextColor() {
        return selectTextColor;
    }

    public void setSelectTextColor(int selectColor) {
        this.selectTextColor = selectColor;
    }

    public Drawable getmUpRedIcon() {
        return mUpRedIcon;
    }

    public void setmUpRedIcon(Drawable mUpRedIcon) {
        this.mUpRedIcon = mUpRedIcon;
    }

    public void setmDIocn(Drawable mDIocn) {
        this.mDIocn = mDIocn;
    }

    public void setmUpIocn(Drawable mUpIocn) {
        this.mUpIocn = mUpIocn;
    }

    public void setmDRedIocn(Drawable mDRedIocn) {
        this.mDRedIocn = mDRedIocn;
    }

    public static class TagGroup{
        private String text;

        private boolean isOpen=false;

        public TagGroup(String text) {
            this.text = text;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }


        public boolean isOpen() {
            return isOpen;
        }

        public void setOpen(boolean open) {
            isOpen = open;
        }
    }
    public TagGroupView setOnTabChangeListener(OnTagClickListener onTabChangeListener) {
        this.onTagClickListener = onTabChangeListener;
        return this;
    }
    public interface OnTagClickListener {
        void onClick(View v, int index, boolean isOpen);
    }
}
